package com.shopping.controller;

import com.shopping.common.ResultCode;
import com.shopping.config.CacheComponent;
import com.shopping.dto.LoginDto;
import com.shopping.dto.RegisterDto;
import com.shopping.dto.SaveUserVo;
import com.shopping.dto.VerificationCodeDto;
import com.shopping.service.UserService;
import com.shopping.util.CommonUtils;
import com.shopping.util.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author 张衍斌
 * @version 1.0
 * @program dada-shopping
 * @description 用户控制器
 * @create 2019-08-05 14:33
 */
@RestController
@RequestMapping("/user")
@Api("用户模块")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "登录接口", notes = "登录接口")
    @PostMapping("/login")
    public String login(@RequestBody LoginDto loginDto) {
        String type = loginDto.getType();
        String account = loginDto.getAccount();
        String code = loginDto.getCode();
        if (CommonUtils.isNull(account) || CommonUtils.isNull(type)) {
            return ResponseData.customResult(ResultCode.paramLost.getCode(), ResultCode.paramLost.getMessage());
        }
        return userService.login(type,account,code);
    }

    @ApiOperation(value = "注册接口", notes = "注册接口")
    @PostMapping("/register")
    public String register(@RequestBody RegisterDto registerDto) {
        String mobile = registerDto.getMobile();
        String verificationCode = registerDto.getVerificationCode();
        if (!CommonUtils.isPhone(mobile) ||
                StringUtils.isBlank(verificationCode)) {
            return ResponseData.customResult(ResultCode.paramLost.getCode(), ResultCode.paramLost.getMessage());
        }
        return userService.register(mobile,verificationCode);
    }

    @ApiOperation(value = "保存个人信息",notes = "保存个人信息")
    @PostMapping("/registerAfter")
    public String registerAfter(@RequestBody SaveUserVo userVo){
        return userService.registerAfter(userVo);
    }


    @ApiOperation(value = "随机验证码",notes = "随机验证码")
    @PostMapping("/getVerificationCode")
    public String getVerificationCode(@RequestBody VerificationCodeDto codeDto){
        String mobile = codeDto.getMobile();
        if(!CommonUtils.isPhone(mobile)){
            return ResponseData.customResult(ResultCode.paramLost.getCode(), ResultCode.paramLost.getMessage());
        }
        return userService.getVerificationCode(mobile);
    }

}
